##################################################
### REPLICATION FILES

### Who Benefits? How Local Ethnic Demography Shapes Political Favoritism in Africa.
# Janina Beiser-McGrath, Carl Müller-Crepon, and Yannick Pengl
# British Journal of Political Science 
##################################################

This replication archive contains all data and code to replicate the tables and figures in our paper and online appendix.

Unfortunately, Harvard Dataverse does not appear to preserve the folder and subfolder structure of our replication materials.
To make sure that everything works without complications, please prepare the following folders and subfolders on your system before you download any R code or dataset:

1.) A main folder called
"BMCCP2000_Replication"

2.) 5 subfolders called:
"data"
"figures"
"logfiles"
"scripts"
"tables"

When this is done, save all four data files in the subfolder "data"
These files are:
"ab_side.RData"
"dhs_epr_frt_side.rds"
"districts_2000_covariates.rds"
"districts_2000.RData"

Finally, save all 13 R scripts in the subfolder "scripts"
These files are:
"analysis_all.R"
"analysis_globals.R"
"analysis_descriptives.R"
"analysis_inclmaps.R"
"analysis_table1.R"
"analysis_table2.R"
"analysis_robchecks.R"
"analysis_ealevel.R"
"analysis_hh_fe.R"
"analysis_frt.R"
"analysis_altoutcomes.R"
"analysis_heterogeneity.R"
"analysis_afrobarometer.R"



In what follows, we explain what these scripts do and provide some guidance on how to replicate either selected parts or the entire material in one go.

If you have any questions, get in touch:
janina.beiser@uni-konstanz.de
carl.mueller-crepon@icr.gess.ethz.ch
yannick.pengl@icr.gess.ethz.ch


##################################################
1.) "analysis_all.R"
Open and run this 'master script' to replicate everything in one go.
The file sources all data preparation and analysis scripts and saves all output in the subfolders "figures" and "tables".
Figures are saved as .pdf or .png. Tables come as .tex files and need to be compiled in Latex.
First install the R libraries listed in the preamble if they are not on your system yet.
Before you start, make sure that you set the "replication.path" in line 55 from "~/Desktop" to wherever you saved our replication folder on your system.

You can also call this script from your command line or terminal by running the following two lines:
cd ~/Desktop/BMMCP2020_Replication/
nohup Rscript scripts/analysis_all.R  >logfiles/analysis_yyyymmdd.log>&1 &

Doing so writes all output from the R console to a file and saves it in the subfolder "logfiles".

The entire replication takes about 120 minutes on Yannick Pengl's 2018 MacBook Pro (2.6 GHz Intel i7, 32 GB DDR4).
You can speed things up by parallelizing the estimation of fixed effects models.
To do so, just change the number of CPUs to be used in line 46 from 1 to whatever number you can afford. 
Note that speed gains will be modest as the main bottleneck is memory rather than processing power. 

################################################## 
2.) "analysis_globals.R"
- defines weighting function that ensures equal weights for each country in the regression analyses  
- defines fixed effects, standard error clustering, and control variables for baseline specifications
- specifies file paths to save output figures and tables
- prepares lines and notes for nice Latex tables

##################################################
3.) "analysis_descriptives.R"
- If you run this script separately, make sure to first run lines 1-69 of "analysis_all.R" (loads data and sources "analysis_globals.R")
- prepares and saves descriptive Figures A1 and A2 as "Descriptive1_grey.pdf" and "country_means.pdf"
- prepares and saves Table A1 (Summary Statistics DHS Data) as "sumstats.tex"
- prepares and saves Table A2 (Regressing Mothers' Individual Co-Ethnicity on District Share) as "SIDE_check.tex"

##################################################
4.) "analysis_inclmaps.R"
- If you run this script separately, make sure to first run lines 1-69 of "analysis_all.R" (loads data and sources "analysis_globals.R")
- prepares and saves district maps of politically included population shares in 2000 for all countries in our sample.
These maps are saved as "incl_map2000_432.png" to "incl_map2000_565.png" and the last three numbers in the file names correspond to COW codes.
- Figure 1 in the main paper includes the year 2000 maps for Côte d'Ivoire (437), Nigeria (475), Uganda (500), and Zambia (551)
- Figure A3 plots all maps not included in Figure 1
- prepares and saves equivalent maps for Uganda in 1965, 1980, and 1995 as "incl_map1965_500.png" etc. 
- These maps are included in Figure 2 in the main paper. 
- Figures 1, 2 and A3 include a color guide that is prepared and saved as incl_map2000_palette.png
- The script also prepares a continental version of the 2000 map saved as "incl_map2000.png" (not included in paper or appendix)
- prepares and saves Appendix Figure A4 (Continental Map of DHS Survey Locations) as "dhs_points.png"
- prepares and saves Appendix Figure A5 (Inclusion of ethnic groups in African governments by country and year) as "epr_timeline_1.png" and "epr_timeline_2.png"

##################################################
5.) "analysis_table1.R"
- If you run this script separately, make sure to first run lines 1-69 of "analysis_all.R" (loads data and sources "analysis_globals.R")
- prepares and runs our baseline models. 
- saves Table 1 as "baseline.tex"
- prepares and saves Figure 3 (Predictions) as "pred_baseline.pdf"
- prepares and saves Figure A6 (Marginal Effects) as "mfx_baseline.pdf"
- prepares and saves Figure A7 (Predictions & Binning Estimates) as "pred_baseline_binning.pdf"
- prepares and saves Figure A8 (Marginal Effects & Binning estimates) as "mfx_baseline_binning.pdf"

################################################## 
6.) "analysis_table2.R"
- If you run this script separately, make sure to first run lines 1-69 of "analysis_all.R" (loads data and sources "analysis_globals.R")
- prepares and runs additional models included in the main paper.
- saves Table 2 as "robustness_trends.tex"

##################################################
7.) "analysis_robchecks.R"
- If you run this script separately, make sure to first run lines 1-69 of "analysis_all.R" (loads data and sources "analysis_globals.R")
- prepares and runs additional models included in the Online Appendix
- saves Table A3 (Different Temporal Lags) as "robustness_lags.tex"
- saves Table A4 (Different Temporal Lags) as "robustness_clusters.tex"
- saves Table A5 (No Weights) as "unweighted.tex"
- saves Table A6 (No Control Variables) as "robustness_nc.tex"
- saves Table A7 (Government Senior Partners) as "m1.2cl.weights_senior.tex"
- saves Table A8 (Ethnic vs. District-level Diff-in-Diff) as "simple_dd.tex"
- saves Table A9 (Pre-Trends) as "pre_trends.tex"

##################################################
8.) "analysis_ealevel.R"
- If you run this script separately, make sure to first run lines 1-69 of "analysis_all.R" (loads data and sources "analysis_globals.R")
- aggregates data at the EA-level
- prepares and runs additional models included in the Online Appendix
- saves Table A10 (Enumeration Areas & Spatial Segregation) as "robustness_ea.tex" 

##################################################
9.) "analysis_hh_fe.R"
- If you run this script separately, make sure to first run lines 1-69 of "analysis_all.R" (loads data and sources "analysis_globals.R")
- calculates descriptive statistics discussed on p. A34 in the Online Appendix 
- prepares and runs models with household and mother fixed effects
- saves Table A14 (Household and Mother FE) as "hh_mo_fe.tex" 

##################################################
10.) "analysis_frt.R"
- If you run this script separately, make sure to first run lines 1-69 of "analysis_all.R" (loads data and sources "analysis_globals.R")
- runs models that define individual and distric-level government co-ethnicity from FRT rather than EPR data
- saves Table A11 (Robustness: FRT Data) as "robustness_frt.tex"
- produces a version of our prediction plot (Figure 3) based on Model 2 in Table A11. Saved as "pred_baseline_frt.pdf"

##################################################
11.) "analysis_altoutcomes.R"
- If you run this script separately, make sure to first run lines 1-69 of "analysis_all.R" (loads data and sources "analysis_globals.R")
- runs models that use alternative DHS outcomes on maternal health care and infant weight
- prepares and saves Figure A9 (Distribution of birthyears in baseline and reduced sample) as "birthyr_dist_altoutcomes.pdf"
- saves Table A12 as "new_outcomes7_no_fe.tex"
- saves Table A13 as "new_outcomes7_both_fe.tex"

##################################################
12.) "analysis_heterogeneity.R"
- If you run this script separately, make sure to first run lines 1-69 of "analysis_all.R" (loads data and sources "analysis_globals.R")
- runs interaction models to test for heterogeneity accross political systems
- saves Table A15 (Heterogeneity: Regime Type & Electoral System) as "heterogeneity.tex"
- saves the top-left panel of Figure A10 as "pred_3IA_polity_low.pdf"
- saves the top-right panel of Figure A10 as "pred_3IA_polity_high.pdf"
- saves the bottom-left panel of Figure A10 as "pred_3IA_polity_diff_co.pdf"
- saves the bottom-right panel of Figure A10 as "pred_3IA_polity_diff_nonco.pdf"
- saves the top-left panel of Figure A11 as "pred_3IA_vdem_low.pdf"
- saves the top-right panel of Figure A11 as "pred_3IA_vdem_high.pdf"
- saves the bottom-left panel of Figure A11 as "pred_3IA_vdem_diff_co.pdf"
- saves the bottom-right panel of Figure A11 as "pred_3IA_vdem_diff_nonco.pdf"
- saves the top-left panel of Figure A12 as "pred_3IA_fptp_no.pdf"
- saves the top-right panel of Figure A12 as "pred_3IA_fptp_yes.pdf"
- saves the bottom-left panel of Figure A12 as "pred_3IA_fptp_diff_co.pdf.pdf"
- saves the bottom-right panel of Figure A12 as "pred_3IA_fptp_diff_nonco.pdf.pdf"
- saves conventional marginal effect plots as:
"mfx_3IA_polity_low.pdf"
"mfx_3IA_polity_high.pdf"
"mfx_3IA_vdem_low.pdf"
"mfx_3IA_vdem_high.pdf"
"mfx_3IA_pr.pdf"
"mfx_3IA_fptp.pdf"

##################################################
13.) "analysis_afrobarometer.R"
- prepares data and runs models with Afrobarometer data on economic hardship and the ease of accessing public services
- prepares and saves Table 3 (Economic hardsip and public services) as "afrobarometer_main.tex"
- prepares and saves Table A16 (Principal Component Analysis: Economic Hardship) as "afrobarometer_econ_pca.tex"
- prepares and saves Table A17 (Principal Component Analysis: Perceived Service Accessibility) as "afrobarometer_difficulty_pca.tex"
- prepares and saves Table A18 (Afrobarometer Summary Statistics) as "afrobarometer_sumstats.tex"
- prepares and saves Table A19 (Economic Hardship Indicators) as "afrobarometer_hardship.tex"
- prepares and saves Table A20 (Ease of Accessing Services) as "afrobarometer_services.tex"

	
	 

